package cn.ekuma.epos.datalogic.define.dao;

import cn.ekuma.data.dao.ModifiedLogDAO;
import cn.ekuma.epos.db.table.I_ProductScore;

import com.openbravo.bean.Product;
import com.openbravo.bean.ProductScore;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataParams;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.PreparedSentence;
import com.openbravo.data.loader.SerializerWriteParams;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

public class ProductScoreDAO extends ModifiedLogDAO<ProductScore> {

	public ProductScoreDAO(I_Session s) {
		super(s);
	}

	@Override
	public TableDefinition getTable() {
		return new TableDefinition(s, "PRODUCT_SCORE", new Field[] {
				new Field(I_ProductScore.ID, Datas.STRING, Formats.STRING),
				new Field(I_ProductScore.PRODUCTID, Datas.STRING, Formats.STRING,Product.class),
				new Field(I_ProductScore.SCORETYPE, Datas.INT, Formats.INT),
				new Field(I_ProductScore.SCORE, Datas.DOUBLE, Formats.DOUBLE),
				new Field(I_ProductScore.LASTMODIFIED, Datas.TIMESTAMP, Formats.TIMESTAMP)
	         }, new int[] {0});
	}

	@Override
	public void writeInsertValues(DataWrite dp, ProductScore obj)
			throws BasicException {
		dp.setString(1,obj.getKey());
		dp.setString(2,obj.getProductId());
		dp.setInt(3,obj.getType());
		dp.setDouble(4,obj.getScore());
		dp.setTimestamp(5,obj.getLastModified());
		
	}

	@Override
	public Class getSuportClass() {
		return ProductScore.class;
	}

	@Override
	public ProductScore readValues(DataRead dr, ProductScore obj)
			throws BasicException {
		if(obj==null)
			obj=new ProductScore();
		obj.setKey(dr.getString(1));
		obj.setProductId(dr.getString(2));
		obj.setType(dr.getInt(3));
		obj.setScore(dr.getDouble(4));
		obj.setLastModified(dr.getTimestamp(5));
		return obj;
	}
	
	public final ProductScore getProductScoreByProductAndType(final String product,final int type)
	throws BasicException {
return (ProductScore) new PreparedSentence(
		s,
		getTableDefinition().getListSQL()
				+ " WHERE PRODUCTID=? AND SCORETYPE=?",
		SerializerWriteParams.INSTANCE, this).find(new DataParams() {
	public void writeValues() throws BasicException {
		setString(1, product);
		setInt(2, type);
	}
});
}

}
